<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>06.state案例1-计数器</title>
    <!--1、引入react核心文件-->
    <script src="./js/react.development.js"></script>
    <!--2、引入react-dom核心文件-->
    <script src="./js/react-dom.development.js"></script>
    <!--3、引入babel转换文件-->
    <script src="./js/babel.min.js"></script>
    <style>
        .ml {
            margin-left: 10px;
        }
    </style>
</head>

<body>
    <!--4、创建容器-->
    <div id="root"></div>
    <!--5、设置脚本类型,相当于里面所有代码都默认处在严格模式下，而严格模式下的函数的this为undefined-->
    <script type="text/babel">
        //6、创建容器对象
        let root = ReactDOM.createRoot(document.querySelector('#root'));
        //7、创建类组件
        let { Component } = React;
        //案例：今天的心情好不好
        class App extends Component {
            state = {
                count: 1
            }
            //每一个类组件必须要有render方法，目的是为了渲染DOM结构
            //render的执行次数为1(初始化的时候)+n(当每一次state的值发生改变的时候)次
            render() {
                //读取状态数据
                let { count } = this.state;
                return <div>
                    <p>计数器:{count}</p>
                    <button onClick={() => this.addCount(1)}>+1</button>
                    <button className="ml" onClick={() => this.addCount(5)}>+5</button>
                    <button className="ml" onClick={() => this.addCount(10)}>+10</button>
                    <button className="ml" onClick={() => this.addCount(15)}>+15</button>
                    <button className="ml" onClick={() => this.addCount(20)}>+20</button>
                    <button className="ml" onClick={this.addCount1(25)}>+25</button>
                </div>
            }
            addCount = function (n) {
                let { count } = this.state;
                this.setState({
                    count: count + n
                })
            }
            addCount1 = num => {
                return () => {
                    // console.log(this)
                    // console.log(num)
                    let { count } = this.state;
                    this.setState({
                        count: count + num
                    })
                }
            }
        }
        //8、渲染
        root.render(<App />); // let s1 = new App()
    </script>
</body>

</html>